/**
 * @class   wosh::gui::WidgetMediaList
 * @brief   Shows Properties (PropertiesProvider) and allow user
 *          to edit then (asynch, remote through bus messages).
 *
 ****************************************************************************
 * @version $Id: WidgetMediaList.h 2748 2010-07-11 22:07:06Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WidgetMediaList_H__
 #define __WOSH_Gui_WidgetMediaList_H__

#include <framework/gui/IWidget.h>
#include <framework/gui/IWidgetSerializable.h>

#include <framework/entertainment/MediaList.h>

#include "ui_WidgetMediaList.h"

using namespace wosh;
using namespace wosh::entertainment;
namespace wosh {
 namespace gui {


class WidgetMediaList :		public QWidget,
							public virtual IWidget, 
							public virtual IWidgetSerializableT<wosh::entertainment::MediaList>
{
	Q_OBJECT

	WOSH_WIDGET_SERIALIZABLE(wosh::gui::WidgetMediaList)

	public:

		WidgetMediaList(QWidget *parent = 0) : QWidget(parent) {
			this->m_ui = new Ui::WidgetMediaList();
			this->m_ui->setupUi(this);

			this->setWindowTitle("MediaList");
			this->getWidgetURI().setName( Utilities::format("WidgetMediaList#%10lu", Utilities::randomLong()) );

			connect(this->m_ui->tableView, SIGNAL(updated()), this->m_ui->wUpBarTracks, SLOT(sayReady()));
			connect(this->m_ui->wUpBarTracks, SIGNAL(updateClicked()), this, SLOT(refreshAll()));

		 }
		virtual ~WidgetMediaList() {
			delete this->m_ui;
		 }

	public slots:
		virtual void refreshAll() {
			this->setWindowTitle( QString("MediaList %1 [%2]").arg(this->getTargetURI().getName().c_str()).arg(this->getTargetURI().getKernelName().c_str()) );
			
			this->m_ui->wwCore_PropertiesProvider->refreshAll();
			
			Request* request_method = new Request( _METHOD_List );
			postMessageForTarget( new Message(request_method) );
		 }

	public:
		void setTargetURI( const URI& target_uri ) {
			this->targetURI = target_uri;
			this->m_ui->wwCore_PropertiesProvider->setTargetURI(target_uri);
		 }

		virtual WRESULT loadObject( const wosh::entertainment::MediaList* object ) {
			if ( object == NULL ) {

				return WRET_OK;
			 }
			setWindowTitle( QString("MediaList #%1").arg(object->getName().c_str()) );
			
			return WRET_OK;
		 }

		virtual WRESULT saveObject( wosh::entertainment::MediaList* object ) const {
			if ( object == NULL ) return WRET_ERR_PARAM;

			return WRET_OK;
		 }
 
		virtual void busMessage( const Message& message, const Bus* ) {
			if ( !message.getContent()->isResponse() ) return;
			if ( !isMessageForMeOrFromTarget(message) )
				return;
			const wosh::Response* response = message.getContent()->asResponse();

			this->m_ui->tableView->evalResponse(response);
		 }
 
	protected:
		void changeEvent(QEvent *e) {
			QWidget::changeEvent(e);
			switch (e->type()) {
			case QEvent::LanguageChange:
				m_ui->retranslateUi(this);
				break;
			default:
				break;
			}
		}

		void closeEvent(QCloseEvent*) {
			 this->deleteLater();
		 }

	private:
		Ui::WidgetMediaList *m_ui;

}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_WidgetMediaList_H__
